#include "Player.h"
#include "../Managers/GameManager.h"
#include "../Managers/InputManager.h"

/*#include "../World/World.h"
#include "../World/Grid.h"
#include "../World/Chunk.h"
#include "../World/Cell.h"*/

#include "../Renderer/DeferredRenderer.h"

#include <float.h>
#include <sstream>

Player::Player(const Vec2i &viewport_size)
	: GameObject(Vec2i(),'@',TCODColor::white), viewport_size(viewport_size*4)
{
	fov = std::make_shared<TCODMap>(this->viewport_size.x(), this->viewport_size.y());
}

Player::~Player()
{
}

void Player::init()
{
	auto input = GameManager::getSingleton()->getInputMgr();
	input->setKeyCallback(TCODK_LEFT,	0, [this](TCOD_key_t input) { if(this->getFOV()->isWalkable((viewport_size/2).x()-1, (viewport_size/2).y())) this->translate(-1, 0); });
	input->setKeyCallback(TCODK_RIGHT,	0, [this](TCOD_key_t input) { if(this->getFOV()->isWalkable((viewport_size/2).x()+1, (viewport_size/2).y())) this->translate( 1, 0); });
	input->setKeyCallback(TCODK_UP,		0, [this](TCOD_key_t input) { if(this->getFOV()->isWalkable((viewport_size/2).x(), (viewport_size/2).y()-1)) this->translate( 0,-1); });
	input->setKeyCallback(TCODK_DOWN,	0, [this](TCOD_key_t input) { if(this->getFOV()->isWalkable((viewport_size/2).x(), (viewport_size/2).y()+1)) this->translate( 0, 1); });
}

void Player::update()
{
	lower_bounds = pos - (viewport_size/2);
	upper_bounds = pos + (viewport_size/2);
}

void Player::render()
{
	auto g_buffer = GameManager::getSingleton()->getRenderer();
	g_buffer->addSymbol(pos, tile, FLT_MAX); //No symbol more important than the player
	g_buffer->shade(pos, Vec3f(1.0f, 1.0f, 1.0f), DeferredRenderer::RL_FOREGROUND, DeferredRenderer::RT_DIFFUSE);
	g_buffer->shade(pos, Vec3f(0.5f, 0.5f, 0.5f), DeferredRenderer::RL_FOREGROUND, DeferredRenderer::RT_SPECULAR);
	g_buffer->setMapInfo(pos, true, true);

	/*auto chunk = GameManager::getSingleton()->getWorld()->getGrid()->getChunkWithinBounds(pos);
	if(chunk == nullptr)
		return;

	auto cell = chunk->getCell(pos);
	if(cell == nullptr)
		return;

	float height = cell->getPos().y();
	g_buffer->light(pos, height+1.8f, fov, Vec3f(0.9f, 0.85f, 0.75f), 10, DeferredRenderer::BLEND_DEFAULT, true);

	std::stringstream ss;
	ss << "P: " << pos.x() << ", " << height << ", " << pos.y();
	GameManager::getSingleton()->getStoryConsole()->print(0,0, ss.str().c_str());*/
}
